home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Plus Special 16
/
AMIGAplus Sonderheft 16 (1998)(ICP)(DE)[!].iso
/
pd
/
anwendungen
/
ispell-3.1.18src
/
addons
/
xspell.shar
/
look.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-01-23
|
6KB
|
281 lines
static char *progid = "look 1.0a, 4 December 92 philipp@res.enst.fr";
/*
* Xspell: Philippe-Andre Prindeville, Telecom Paris 4 December 1992
*
* After cursing ispell numerous times for not having a
* simple command line interface like "look" for searching
* for words, I wrote one. Most of the code was recycled
* from Xspell anyway (I should plug it here but...).
*
* You type:
*
* look [-d dictionary] word1 [word2 ... wordn ]
*
* and, in the best of UNIX tradition, it will *not* print
* out the word if it likes it, will print out a list of
* alternatives for dubious words, and will print a stupid
* message (well after 10 minutes hacking I wasn't going
* to spend 3 hours worrying about "user-friendly" error
* messages) saying it didn't like the word...
*
* Debugging is simple. If you have patches, by all means
* send them to me.
*
* Enjoy,
* -Philip
*/
#include "config.h"
#include "ispell.h"
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#ifdef DEBUG
#define dprintf printf
#define dputc putchar
#else
#define dprintf _dprintf
#define dputc(x)
#endif
extern char *optarg;
extern int optind, opterr, optopt;
char *lang = "english";
char *fmt = NULL;
int verbose = 0;
static FILE *srvin, *srvout;
static char possibilities[MAXPOSSIBLE][INPUTWORDLEN + MAXAFFIXLEN];
static int pcount;
static void
Connect()
{
int srv[2], clnt[2];
int pid, n, argc;
char buf[BUFSIZ];
char *argv[10];
pipe(srv);
pipe(clnt);
if ((pid = fork()) == -1) {
perror("fork");
exit(1);
} else if (! pid) {
dup2(srv[0], 0);
close(srv[0]); close(srv[1]);
dup2(clnt[1], 1);
close(clnt[0]); close(clnt[1]);
argc = 0;
argv[argc++] = "ispell";
argv[argc++] = "-d";
argv[argc++] = lang;
if (! strcmp(lang, "french"))
argv[argc++] = "-Tlatin1";
#if 0
argv[argc++] = "-P";
#endif
argv[argc++] = "-S";
argv[argc++] = "-a";
argv[argc++] = NULL;
execvp("ispell", argv);
perror("execl: ispell");
exit(1);
} else {
close(clnt[1]);
close(srv[0]);
if (! (srvin = fdopen(clnt[0], "r"))
|| ! (srvout = fdopen(srv[1], "w"))) {
fprintf(stderr, "fdopen: cannot open stream\n");
exit(1);
}
/*
* get hello banner
*/
n = fgets(buf, sizeof(buf) - 1, srvin);
dprintf(buf);
if (verbose)
fputs(buf, stdout);
}
}
static void
SessionEnd()
{
dprintf(">>> #\n");
fprintf(srvout, "#\n");
fclose(srvout);
fclose(srvin);
}
static void
Choices (word, count, poss)
char *word;
int count;
char poss[MAXPOSSIBLE][INPUTWORDLEN + MAXAFFIXLEN];
{
int i;
printf("%s:", word);
for (i = 0; i < count; ++i)
if (! strpbrk(poss[i], " -")) printf(" %s", poss[i]);
putchar('\n');
}
static void
Clueless (word)
char *word;
{
printf("%s ??? Totally bogus, bro'\n", word);
}
static void
ReadBack ()
{
int c, m, n, reliable;
long pos;
char word[INPUTWORDLEN];
for (; ; ) {
dprintf("<<< ");
c = fgetc(srvin);
switch (c) {
case EOF: /* very unexpected */
dprintf("EOF\n");
return;
case '\n': /* done! */
dprintf("\\n\n");
break;
case '*': /* word is OK */
dputc('*');
c = fgetc(srvin);
dputc(c);
break;
case '+': /* found derivative */
dputc('+');
(void)fgets(word, sizeof(word), srvin);
dprintf("%s", word);
break;
case '&': /* word is dubious */
dputc('&');
n = fscanf(srvin, " %s %d %d", word, &reliable, &pos);
pos--; /* correct for uparrow we added */
dprintf("\"%s\" at %d, %d choices:", word,
pos, reliable);
for (n = 0; ; ++n) {
c = fgetc(srvin); /* eat colon or comma */
if (c == '\n') break;
fscanf(srvin, " %[^,\n]", possibilities[n]);
dprintf(" \"%s\"", possibilities[n]); fflush(stdout);
}
/* newline already eaten */
dputc(c);
pcount = n;
Choices(word, pcount, possibilities);
break;
case '?':
dputc('?');
n = fscanf(srvin, " %s %d %d", word, &reliable, &pos);
pos--; /* correct for uparrow we added */
dprintf(" \"%s\" at %d, %d choices:",
word, pos, reliable);
for (n = 0; ; ++n) {
c = fgetc(srvin); /* eat colon or comma */
if (c == '\n') break;
fscanf(srvin, " %[^,\n]", possibilities[n]);
dprintf(" \"%s\"", possibilities[n]); fflush(stdout);
}
/* newline already eaten */
dputc('\n');
pcount = n;
Choices(word, pcount, possibilities);
break;
case '#':
dputc('#');
n = fscanf(srvin, " %s %d", word, &pos);
pos--; /* correct for uparrow we added */
dprintf(" \"%s\" at %d\n", word, pos);
fgetc(srvin);
pcount = 0;
Clueless(word);
break;
case '-': /* gak! */
default:
dputc(c);
fgets(word, sizeof(word), srvin);
dprintf(" %s", word);
fprintf(stderr, "Error: read '%c' on connection\n", c);
exit(1);
break;
}
}
}
main(argc, argv)
char *argv[];
{
int i, c;
char *myname, *slash;
char *tmp;
myname = argv[0];
if (slash = strrchr(myname, '/'))
myname = ++slash;
while ((c = getopt(argc, argv, "vd:")) != EOF)
switch (c) {
case 'd': /* dictionary */
lang = optarg;
break;
case 'v':
++verbose;
break;
case '?':
usage: fprintf(stderr, "usage: %s [-v] [-d dictionary] word1 [word2 ... wordn]\n", myname);
exit(1);
}
if (!verbose && optind == argc) goto usage;
Connect();
dprintf(">>> ^");
fputc('^', srvout);
for (i = optind; i < argc; ++i) {
dprintf(" %s", argv[i]);
fprintf(srvout, " %s", argv[i]);
}
dputc('\n');
fputc('\n', srvout);
fclose(srvout);
ReadBack();
SessionEnd();
exit(0);
}
#ifndef DEBUG
dprintf()
{
}
#endif